import 'package:flutter/material.dart';

import 'comm/left_icon_widget.dart';

class SearchWidget extends StatefulWidget {
  Function onTapFuns;
  Widget child;
  String tipsText;
  SearchWidget({Key key, this.onTapFuns, this.child,this.tipsText="请输入"}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _SearchWidget();
  }
}

class _SearchWidget extends State<SearchWidget> {
  bool isEditor = false; //是否可编辑 true：编辑状态;false：不可编辑状态
  Widget sIconWid = null;

  TextEditingController searchContro = new TextEditingController();

  void setEditor() {
    setState(() {
      isEditor = !isEditor;
    });
    setSIcon();
  }

  void setSIcon() {
    setState(() {
      if (isEditor) {
        sIconWid = GestureDetector(
          child: Icon(
            Icons.cancel,
            color: Colors.black12,
          ),
          onTap: () {
            searchContro.clear();
            setEditor();
          },
        );
      } else {
        sIconWid = null;
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        elevation: 0.4,
        backgroundColor: Color(0XFF00A5F1),
        automaticallyImplyLeading: false,
        leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
        title: Container(
          width: MediaQuery.of(context).size.width - 90,
          margin: EdgeInsets.only(left: 0, right: 0, top: 8, bottom: 8),
          padding: EdgeInsets.only(left: 20, top: 8, bottom: 8,right: 5),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(25.0),
          ),
          child: TextField(
            autofocus: false,
            controller: searchContro,
            textInputAction: TextInputAction.search,
            decoration: new InputDecoration(
                hintText: widget.tipsText,
              contentPadding: EdgeInsets.all(0),
                hintStyle: TextStyle(color: Colors.black26,fontSize: 14),
                border: InputBorder.none,),
            onChanged: (val) {
              if (!this.isEditor && val != "") {
                setEditor();
              }
              if (val == "") {
                setEditor();
              }
              print(val);
            },
            onEditingComplete: () {
              widget.onTapFuns(searchContro.value.text);
            },
          ),
        ),
        actions: <Widget>[
          Center(
            child: Container(
              width: 50,
              margin: EdgeInsets.only(left: 0),
              padding: EdgeInsets.only(left: 10, right: 10),
              child: GestureDetector(
                onTap: () {
                  widget.onTapFuns(searchContro.value.text);
                },
                child: Text("搜索", style: TextStyle(fontSize: 13)),
              ),
            ),
          ),
        ],
      ),
      body: Scrollbar(
//        child: ListView.separated(
//          itemCount: 60,
//          itemBuilder: (BuildContext context, int index){
//            return ListTile(title: Text("查询结果${index+1}"),);
//          },
//          separatorBuilder:(BuildContext context, int index){
//            return Divider(color: Colors.black26,);
//          }
//        ),
        child: widget.child,
      ),
    );
  }
}
